package ac.beiwo.baas.console.controller;

import ac.beiwo.baas.admin.entity.App;
import ac.beiwo.baas.admin.service.AppService;
import ac.beiwo.baas.admin.service.StatService;
import ac.beiwo.baas.common.entity.SimpleError;
import ac.beiwo.baas.console.entiry.AppInfo;
import ac.beiwo.baas.console.util.NumberUtil;
import ac.beiwo.baas.object.service.ObjectService;
import ac.beiwo.baas.user.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by Codi on 15/11/19.
 */
@Controller
@RequestMapping(value = "/console/app")
public class ConsoleAppController {

    @Autowired
    private AppService appService;
    @Autowired
    private StatService statService;
    @Autowired
    private ObjectService objectService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView list() throws SimpleError {
        ModelAndView mv = new ModelAndView();
        List<App> apps = appService.list();
        List<AppInfo> appInfos = new LinkedList<>();
        for (App app : apps) {
            AppInfo appInfo = new AppInfo();
            appInfos.add(appInfo);
            BeanUtils.copyProperties(app, appInfo);
            //计算用户总数
            long userCount = objectService.count(app.getId(), UserService.USER_CLASS_NAME, null);
            appInfo.setUserCount(NumberUtil.shortNumber(userCount, true));
            //请求数总和
            long yesterday = statService.getYesterdayApiCount(app.getId());
            appInfo.setYesterday(NumberUtil.shortNumber(yesterday, true));
            long month = statService.getCurrentMonthApiCount(app.getId());
            appInfo.setCurrentMonth(NumberUtil.shortNumber(month, true));
        }
        mv.addObject("apps", appInfos);
        mv.setViewName("apps");
        return mv;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView add() {
        return new ModelAndView("app/add");
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ModelAndView create(App app) throws SimpleError {
        appService.insert(app);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("redirect:/console/app/");
        return mv;
    }

    @RequestMapping(value = "/{appId}/setting", method = RequestMethod.GET)
    public ModelAndView setting(@PathVariable("appId") String appId) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("app/setting");
        return mv;
    }

    @RequestMapping(value = "/{appId}/data", method = RequestMethod.GET)
    public ModelAndView data(@PathVariable("appId") String appId) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("app/data");
        return mv;
    }

}
